package com.yutel.amote;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.yutel.amote.packet.AbstractPacket;
import com.yutel.amote.packet.ClientHelper;
import com.yutel.amote.service.AmoteConstant;

public class BoardActivity extends Activity implements OnClickListener {
	final String TAG = "Borad";
	ClientHelper ch = null;
	ImageView iv = null;
	int FLING_MIN_VELOCITY = 10;
	int FLING_MIN_DISTANCE = 10;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.touchpad);
		AmoteConstant con = AmoteConstant.getInstance(this);
		ch = new ClientHelper(con.getKey("serverIP"), con.getKey("serverPort"));
		iv = (ImageView) findViewById(R.id.touch);
		iv.setLongClickable(true);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.buttonDown:
			Log.d(TAG, "buttonDown");
			break;
		case R.id.buttonUp:
			Log.d(TAG, "buttonUp");
			break;
		case R.id.buttonLeft:
			Log.d(TAG, "buttonLeft");
			break;
		case R.id.buttonRight:
			Log.d(TAG, "buttonRight");
			break;
		case R.id.buttonMenu:
			Log.d(TAG, "buttonMenu");
			break;
		case R.id.buttonHome:
			Log.d(TAG, "buttonHome");
			break;
		case R.id.buttonFrontPage:
			Log.d(TAG, "buttonFrontPage");
			break;
		case R.id.buttonNextPage:
			Log.d(TAG, "buttonNextPage");
			break;
		default:
			Log.e(TAG, "view=" + v.getId());
			break;
		}

	}

	public void sendMsg(AbstractPacket ap) {
		if (!ch.send(ap)) {
			AlertDialog.Builder builder = new AlertDialog.Builder(
					BoardActivity.this);
			builder.setTitle("警告")
					.setMessage("服务端无法连接,请重新设置或检查服务端!")
					.setCancelable(false)
					.setPositiveButton("确认",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									Intent control = new Intent(
											BoardActivity.this,
											ClientCfgActivity.class);
									startActivity(control);
								}
							});
			builder.show();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.control, menu);
		menu.removeItem(R.id.menui_server_control);
		menu.removeItem(R.id.menui_client_touch);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menui_appType:
			Intent intent = new Intent(BoardActivity.this,
					ApptypeActivity.class);
			startActivity(intent);
			break;
		case R.id.menui_server_control:
			Log.d(TAG, "menui_server_control");
			break;
		case R.id.menui_client_touch:
			Log.d(TAG, "menui_server_control");
			break;
		case R.id.menui_touch_config:
			Intent control = new Intent(BoardActivity.this,
					ClientCfgActivity.class);
			startActivity(control);
			break;
		case R.id.menui_client_sensor:
			Intent sensor = new Intent(BoardActivity.this, SensorActivity.class);
			startActivity(sensor);
			break;
		case R.id.menui_client_borad:
			Log.d(TAG, "menui_client_borad");
			break;
		default:
			Log.e(TAG, "default=" + item.getItemId());
		}
		return true;
	}

	public void showMsg(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}
}